import com.cy.ActivityApplication;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生请假测试
 *
 * @author wxhn1
 * @since 2024-01-09 14:31
 */
@SpringBootTest(classes = ActivityApplication.class)
@Slf4j
public class 学生请假测试 {

	@Autowired
	RepositoryService ps;

	@Test
	public void 部署流程() {
		Deployment deploy = ps.createDeployment().addClasspathResource("学生请假.bpmn20.xml").deploy();
		System.out.println(deploy.getId());
	}


	@Autowired
	RuntimeService runtimeService;
	@Test
	public void 启动流程() {
		Map<String,Object> map = new HashMap<>();
		map.put("name","刚哥");
		// 根据流程定义的ID启动流程
		ProcessInstance instance = runtimeService.startProcessInstanceByKey("qj","qj:1:27495868-39b0-11ef-81a5-12b1dfd5769f",map);
		// 输出内容
		System.out.println("流程定义ID:" + instance.getProcessDefinitionId());
		System.out.println("流程实例的ID:" + instance.getId());
//        taskService.setOwner();
	}


	@Autowired
	TaskService taskService;

	@Test
	public void 执行待审核的任务() {
		TaskQuery query = taskService.createTaskQuery();
		List<Task> taskList = query.
			processInstanceBusinessKey("qj:1:27495868-39b0-11ef-81a5-12b1dfd5769f"). // 以业务id作为条件
//			processInstanceBusinessKey("qj:10086"). // 以业务id作为条件
//                processDefinitionKey("qj"). // 以流程id作为条件
//                taskAssignee("张三"). // 以任务办理人作为条件
	list();
		// 3.输出任务信息
		for (Task task : taskList) {
			System.out.println("任务ID:"+task.getId());
			System.out.println("任务名称:"+task.getName());
			System.out.println("任务的创建时间:"+task.getCreateTime());
			System.out.println("任务的办理人:"+task.getAssignee());
			System.out.println("流程实例ID："+task.getProcessInstanceId());
			System.out.println("执行对象ID:"+task.getExecutionId());
			System.out.println("流程定义ID:"+task.getProcessDefinitionId());
		}
	}


	//办理任务(主要操作ACT_RU_EXECUTION、ACT_RU_TASK表)
	@Test
	public void 办理任务() {
		// processInstanceBusinessKey 业务id
		// taskAssignee 任务处理人
		Task task = taskService.createTaskQuery().
			processInstanceBusinessKey("qj:1:27495868-39b0-11ef-81a5-12b1dfd5769f")
//			processInstanceBusinessKey("qj:10086")
//                .taskAssignee("刚哥")
			.singleResult();
		// 设置审批意见
		 taskService.addComment(task.getId(),task.getProcessInstanceId(),"同意");
		// 任务id 完成任务进入下一步
		taskService.complete(task.getId());
	}



	@Autowired
	HistoryService historyService;
	/**
	 * 历史活动实例查询
	 */
	@Test
	public void queryHistoryTask() {
		List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery() // 创建历史活动实例查询
//                .processInstanceId("b6d83887-ad4b-11ee-b76f-005056c00008") // 执行流程实例id
			.processInstanceBusinessKey("qj:1:27495868-39b0-11ef-81a5-12b1dfd5769f")  //根据业务id，查询当前业务下创建的流程实例
//			.processInstanceBusinessKey("qj:10086")  //根据业务id，查询当前业务下创建的流程实例
			.orderByTaskCreateTime()
			.asc()
			.list();
		for (HistoricTaskInstance hai : list) {
			System.out.println("活动ID:" + hai.getId());
			System.out.println("流程实例ID:" + hai.getProcessInstanceId());
			System.out.println("活动名称：" + hai.getName());
			System.out.println("办理人：" + hai.getAssignee());
			System.out.println("开始时间：" + hai.getStartTime());
			System.out.println("结束时间：" + hai.getEndTime());


			List<Comment> taskComments = taskService.getTaskComments(hai.getId());
			for (Comment taskComment : taskComments) {
				System.out.println("---"+taskComment.toString());
				System.out.println("审批意见:" + taskComment.getFullMessage());
			}
		}
	}


	@Test
	public void 根据业务id判断流程是否结束(){
		ProcessInstance pi = runtimeService.createProcessInstanceQuery().
			processInstanceBusinessKey("qj:1:27495868-39b0-11ef-81a5-12b1dfd5769f").singleResult();
		System.out.println(pi==null?"结束":"未结束");
	}


	/**
	 * 查询历史流程变量
	 */
	@Test
	public void queryHisProVariable(){
		List<HistoricVariableInstance> historicVariableInstanceList = historyService
			.createHistoricVariableInstanceQuery().
			processInstanceId("78042512-ad6a-11ee-bb19-005056c00008").list();
		for(HistoricVariableInstance historicVariableInstance: historicVariableInstanceList){
			System.out.println("历史流程变量id: "+historicVariableInstance.getId());
			System.out.println("历史流程变量名称: "+historicVariableInstance.getVariableName());
			System.out.println("历史流程变量值: "+historicVariableInstance.getValue());
			System.out.println("==================================================");
		}
	}


}
